//
//  WatchTimer.swift
//  Swift-30-demo
//
//  Created by apple on 2021/11/23.
//

import UIKit

class WatchTimer: UIViewController {
    var result: UILabel!
    var timer: Timer!
    var lableNumber: Double = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "定时器"
        self.view.backgroundColor = UIColor.white
        // Do any additional setup after loading the view.
        
        result = UILabel()
        result.textAlignment = .center
        self.view.addSubview(result)
        result.textColor = UIColor.black
        result.snp.makeConstraints { (make) in
            make.width.greaterThanOrEqualTo(100)
            make.height.equalTo(50)
            make.centerX.equalTo(self.view)
            make.top.equalTo(100 + navgationHeight)
        }
        result.font = UIFont.systemFont(ofSize: 30)
        result.text = "0"
        
        let startButton:UIButton = UIButton(type: .custom)
        startButton.setTitle("Start", for: .normal)
        startButton.backgroundColor = UIColor.init(red: 1, green: 101/255.0, blue: 27/255.0, alpha: 1)
        self.view.addSubview(startButton)
        startButton.snp.makeConstraints { (make) in
            make.top.equalTo(self.view).offset(300)
            make.leading.equalTo(self.view).offset(0)
            make.bottom.equalTo(self.view).offset(-150)
            make.width.equalTo(self.view.frame.width / 2)
        }
        startButton.addTarget(self, action: #selector(startHandler), for: .touchUpInside)
        
        let endButton = UIButton(type: .custom)
        endButton.setTitle("End", for: .normal)
        endButton.backgroundColor = UIColor.purple
        endButton.addTarget(self, action: #selector(stopHandler), for: .touchUpInside)
        self.view.addSubview(endButton)
        endButton.snp.makeConstraints { make in
            make.top.equalTo(startButton)
            make.leading.equalTo(startButton.snp.trailing)
            make.bottom.equalTo(self.view).offset(-150)
            make.trailing.equalTo(self.view)
        }
        
        let resetButton = UIButton(type: .custom)
        resetButton.setTitle("Reset", for: .normal)
        resetButton.backgroundColor = UIColor.green
        resetButton.addTarget(self, action: #selector(resetHandler), for: .touchUpInside)
        self.view.addSubview(resetButton)
        resetButton.snp.makeConstraints { make in
            make.top.equalTo(startButton.snp.bottom)
            make.leading.equalTo(self.view)
            make.trailing.equalTo(self.view)
            make.bottom.equalTo(self.view)
        }
    }
    

    @objc func startHandler() {
        if timer != nil {
            stopHandler()
        }
        timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true, block: { Timer in
            self.lableNumber = self.lableNumber + 0.1
            self.result.text = String(format: "%.1f", self.lableNumber)
        })
    }

    @objc func stopHandler() {
        guard let timerForDistory = timer else { return }
        timerForDistory.invalidate()
    }
     
     @objc func resetHandler() {
         lableNumber = 0
         result.text = "0"
     }
}
